package com.eedi.framework.tenant.controller.sys;

import com.eedi.framework.tenant.controller.sys.vo.tenant.*;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.tenant.controller.sys.vo.tenant.*;
import com.eedi.framework.tenant.dal.dataobject.TenantDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.annotation.security.PermitAll;

import com.eedi.framework.tenant.service.TenantService;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "系统界面 - 租户")
@RestController
@RequestMapping("/tenant")
public class AdminTenantController {

    @Resource
    private TenantService tenantService;

    @GetMapping("/get-id-by-name")
    @PermitAll
    @Operation(summary = "使用租户名，获得租户编号", description = "登录界面，根据用户的租户名，获得租户编号")
    @Parameter(name = "sysTenantName", description = "租户名", required = true, example = "1024")
    public CommonResult<String> getTenantIdByName(@RequestParam("sysTenantName") String sysTenantName) {
        TenantDO tenant = tenantService.getTenantByName(sysTenantName);
        return success(tenant != null ? tenant.getSysTenantId() : null);
    }

    @GetMapping("/get-by-website")
    @PermitAll
    @Operation(summary = "使用域名，获得租户信息", description = "登录界面，根据用户的域名，获得租户信息")
    @Parameter(name = "sysTenantWebsite", description = "域名", required = true, example = "www.iocoder.cn")
    public CommonResult<TenantSimpleResp> getTenantByWebsite(@RequestParam("sysTenantWebsite") String sysTenantWebsite) {
        TenantDO tenant = tenantService.getTenantByWebsite(sysTenantWebsite);
        return success(BeanUtils.toBean(tenant, TenantSimpleResp.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建租户")
//    @PreAuthorize("@ss.hasPermission('sys:tenant:create')")
    public CommonResult<String> createTenant( @RequestBody TenantSaveReq createReqVO) {
        return success(tenantService.createTenant(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户")
//    @PreAuthorize("@ss.hasPermission('sys:tenant:update')")
    public CommonResult<Boolean> updateTenant( @RequestBody TenantUpReq updateReqVO) {
        tenantService.updateTenant(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除租户")
//    @Parameter(name = "sysTenantId", description = "编号", required = true, example = "1024")
////    @PreAuthorize("@ss.hasPermission('sys:tenant:delete')")
//    public CommonResult<Boolean> deleteTenant(@RequestParam("sysTenantId") String sysTenantId) {
//        tenantService.deleteTenant(sysTenantId);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得租户")
    @Parameter(name = "sysTenantId", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('sys:tenant:query')")
    public CommonResult<TenantResp> getTenant(@RequestParam("sysTenantId") String sysTenantId) {
        TenantDO tenant = tenantService.getTenant(sysTenantId);
        return success(BeanUtils.toBean(tenant, TenantResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户分页")
//    @PreAuthorize("@ss.hasPermission('sys:tenant:query')")
    public CommonResult<PageResult<TenantResp>> getTenantPage( TenantPageReq pageVO) {
        PageResult<TenantDO> pageResult = tenantService.getTenantPage(pageVO);
        return success(BeanUtils.toBean(pageResult, TenantResp.class));
    }


}
